import Utils from './Common'

class SqlTool {
  static QUERY_SQL = 'SELECT * FROM '
  static INSERT_SQL = 'INSERT INTO '
  static DELETE_SQL = 'DELETE FROM '
  static UPDATE_SQL = 'UPDATE '
  static TABLES = {
    SYS_USER_PERMISSION: 'sys_user_permission',
    BUTTONS: [
      { key: 'add', name: '新增', icon: 'el-icon-circle-plus-outline' },
      { key: 'edit', name: '编辑', icon: 'el-icon-edit' },
      { key: 'delete', name: '删除', icon: 'el-icon-delete' },
      { key: 'find', name: '查询', icon: 'el-icon-search' },
      { key: 'configTableHeader', name: '配置表头', icon: 'el-icon-setting' }
    ]
  }

  BuildQuerySql () {
    return this
  }

  /**
   * 生成查询语句
   * @param TableName
   * @param Page
   * @param Size
   * @param Where
   * @returns {string}
   * @constructor
   */
  static GeneratedQuerySql (TableName, Page, Size, Where, OrderBy = '') {
    let _sql = this.QUERY_SQL + TableName
    if (!Utils.IsEmpty(Where)) {
      _sql += ' Where ' + Where
    }
    if (!Utils.IsEmpty(OrderBy)) {
      _sql += ' order by ' + OrderBy
    }
    if (!Utils.IsEmpty(Page) && !Utils.IsEmpty(Size)) {
      _sql += ' limit ' + (Page - 1) * Size + ',' + Size
    }
    return _sql
  }

  static GeneratedQuerySql2 (QUERY_SQL, Page, Size, Where, OrderBy = '') {
    let _sql = QUERY_SQL
    if (!Utils.IsEmpty(Where)) {
      _sql += ' Where ' + Where
    }
    if (!Utils.IsEmpty(OrderBy)) {
      _sql += ' order by ' + OrderBy
    }
    if (!Utils.IsEmpty(Page) && !Utils.IsEmpty(Size)) {
      _sql += ' limit ' + (Page - 1) * Size + ',' + Size
    }
    return _sql
  }

  /**
   * 插入语句
   * @param Table
   * @param Data
   * @returns {string}
   * @constructor
   */
  static GeneratedInsertSql (Table, Data) {
    const Fields = []
    const Values = []
    for (const datum of Data) {
      if (datum.pk === 1) {
        continue
      }
      Fields.push(datum.name)
      if (datum.name == 'create_time') {
        datum.Value = Utils.dateFormatStr(new Date())
      }
      if (datum.name == 'create_user') {
        const ui = Utils.GetLoginUserInfo();
        if (!Utils.IsEmpty(ui)) {
          datum.Value = ui.name
        }
      }
      Values.push(`'${datum.Value}'`)
    }
    const _SQL = this.INSERT_SQL + Table.name + ' (' + Fields.join(',') + ') VALUES(' + Values.join(',') + ')'
    return _SQL
  }

  /**
   * 更新语句
   * @param Table
   * @param Data
   * @returns {string}
   * @constructor
   */
  static GeneratedUpdateSql (Table, Data) {
    const Fields = []
    let Where = 'id='
    for (const datum of Data) {
      if (datum.pk === 1) {
        Where += datum.Value
        continue
      }
      Fields.push(datum.name + '=' + `'${datum.Value}'`)
    }
    const _SQL = this.UPDATE_SQL + Table.name + ' set ' + Fields.join(',') + ' WHERE ' + Where
    return _SQL
  }

  static GeneratedDeleteSql (Table, Where) {
    const _SQL = this.DELETE_SQL + Table.name + ' WHERE ' + Where
    return _SQL
  }

  /**
   * 映射表使用
   * 只在重命名中使用
   * @param Table
   * @param Data
   * @constructor
   */
  static SaveMappingSql (Table, Data) {

  }
}

export default SqlTool
